package com.itheima.takeout.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itheima.takeout.R;
import com.itheima.takeout.model.net.bean.GoodsTypeInfo;
import com.itheima.takeout.ui.fragment.GoodsFragment;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Apple on 2016/9/3.
 */
public class GoodsTypeAdapter extends RecyclerView.Adapter {

    private Context context;
    public List<GoodsTypeInfo> goodsTypeInfoList;

    public int selectedPosition = 0;


    public void setGoodsTypeInfoList(List<GoodsTypeInfo> goodsTypeInfoList) {
        this.goodsTypeInfoList = goodsTypeInfoList;
        notifyDataSetChanged();
    }
    private GoodsFragment goodsFragment;
    public GoodsTypeAdapter(GoodsFragment goodsFragment,Context context, List<GoodsTypeInfo> goodsTypeInfoList) {
        this.goodsFragment = goodsFragment;
        this.context = context;
        this.goodsTypeInfoList = goodsTypeInfoList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_type, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        GoodsTypeInfo goodsTypeInfo = goodsTypeInfoList.get(position);
        if(goodsTypeInfo.getCount() == 0){
            viewHolder.tvCount.setVisibility(View.INVISIBLE);
        }else{
            viewHolder.tvCount.setVisibility(View.VISIBLE);
            viewHolder.tvCount.setText(goodsTypeInfo.getCount()+"");
        }

        viewHolder.type.setText(goodsTypeInfo.getName());

        if(selectedPosition == position){
            viewHolder.itemView.setBackgroundColor(Color.WHITE);
            viewHolder.type.setTextColor(Color.BLACK);
            viewHolder.type.setTypeface(Typeface.DEFAULT_BOLD);//粗体
        }else{
            viewHolder.itemView.setBackgroundColor(Color.parseColor("#b9dedcdc"));
            viewHolder.type.setTextColor(Color.GRAY);
            viewHolder.type.setTypeface(Typeface.DEFAULT);//粗体
        }
        viewHolder.setOnItemClick(position);
    }

    @Override
    public int getItemCount() {
        return goodsTypeInfoList != null ? goodsTypeInfoList.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @InjectView(R.id.tvCount)
        TextView tvCount;
        @InjectView(R.id.type)
        TextView type;

        ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

        public void setOnItemClick(final int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectedPosition = position;
                    notifyDataSetChanged();
                   //获取类型id
                    int typeId = goodsTypeInfoList.get(position).getId();
                    goodsFragment.onItemTypeClick(typeId);
                }
            });
        }
    }
}
